import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store";
import asc from "@utils/asc.js";
Vue.use(VueRouter);

const index = () =>
    import ("../views/index");
const home = () =>
    import ("../views/home/home");
const datav = () =>
    import ("../views/datav/datav");
const mainframe = () =>
    import ("../views/mainframe/mainframe");
const buildingDetails = () =>
    import ("../views/buildingDetails/buildingDetails");
const videoSurveillance = () =>
    import ("../views/videoSurveillance/videoSurveillance");
const alarmReceiver = () =>
    import ("../views/alarmReceiver/alarmReceiver");
const alarm = () =>
    import ("../views/warnList/warnList");
const alarmDetails = () =>
    import ("../views/warnList/alarmDetails");
// const villageManage = () =>
//     import (
//         '../views/villageManage/villageManage'
//     );
const buildingsManage = () =>
    import ("../views/buildingsManage/buildingsManage");
const drawing = () =>
    import ("../views/drawing/drawing");
const floorsManage = () =>
    import ("../views/floorsManage/floorsManage");
const mountPointsDefine = () =>
    import ("../views/mountPointsDefine/mountPointsDefine");
const spacesManage = () =>
    import ("../views/spacesManage/spacesManage");

// const isolateManage = () =>
//     import (
//         '../views/isolateManage/isolateManage'
//     );
const terminal = () =>
    import ("../views/deviceList/terminal/terminal");

const smoke = () =>
    import ("../views/deviceList/smoke/smoke");

const command = () =>
    import ("../views/deviceList/command/command");

const login = () =>
    import ("../views/login/login");

// const locateMonitor = () =>
//     import (
//         '../views/locateMonitor/locateMonitor'
//     );
const orgList = () =>
    import ("../views/orgManage/orgList");
const planar = () =>
    import ("../views/orgManage/planar/planar");
const logLogin = () =>
    import ("../views/log/logininfor");
const logPushlog = () =>
    import ("../views/log/pushlog");
const installLog = () =>
    import ("../views/log/install");
const logOperate = () =>
    import ("../views/log/operlog");
const role = () =>
    import ("../views/system/role");
const permission = () =>
    import ("../views/system/permission");
const member = () =>
    import ("../views/system/member");
const personal = () =>
    import ("../views/personal2/personal");
const productType = () =>
    import ("../views/productType/productType");
const deviceList = () =>
    import ("../views/deviceList/deviceList");
const tenant = () =>
    import ("../views/tenant/tenantList");
const systemSite = () =>
    import ("../views/system/site");
const systemSetup = () =>
    import ("../views/system/setup");
const product = () =>
    import ("../views/deviceList/product/product");
const region = () =>
    import ("../views/region/region");
const manufacturer = () =>
    import ("../views/deviceList/manufacturer/manufacturer");

const serviceList = () =>
    import ("../views/message/service/service");

const smsLogList = () =>
    import ("../views/message/smsLog/smsLog");

const voiceList = () =>
    import ("../views/message/voice/voice");

const voiceLogList = () =>
    import ("../views/message/voiceLog/voiceLog");

const ceshi = () =>
    import ("../views/ceshi/ceshi");

const accountWebpage = () =>
    import ("../views/account-webpage/account-webpage");

const liveMacrovision = () =>
    import ("../views/liveMacrovision/liveMacrovision");

const loginProtocol = () =>
    import ("../views/loginProtocol/loginProtocol");

const serviceTerms = () =>
    import ("../views/serviceTerms/serviceTerms");

const privacy = () =>
    import ("../views/privacy/privacy");

const HomeView = () =>
    import ("../views/HomeView/HomeView");

const notFound = () =>
    import ("../views/notFound/notFound");

const hidden = () =>
    import ("../views/warnList/hidden/hidden");

const maintenance = () =>
    import ("../views/maintenance/maintenance");

const exportTask = () =>
    import ("../views/system/task");

const dataExport = () =>
    import ("../views/export/export");

const dict = () =>
    import ("../views/system/dict/dict");

const dictData = () =>
    import ("../views/system/dict-data/dictData");

const subscribe = () =>
    import ("../views/org/subscribe/subscribe");

const logpush = () =>
    import ("../views/log/subscribe");

const workOrder = () =>
    import ("../views/maintenance/workOrder/workOrder");

const alarmworkOrder = () =>
    import ("../views/warnList/workOrder/workOrder");

const rule = () =>
    import ("../views/ruleEngine/rule");

const alarmStatics = () =>
    import ("../views/statics/alarmStatics");

const hiddenDangerStatics = () =>
    import ("../views/statics/hiddenDangerStatics");

const categoryManagement = () =>
    import ("../views/access/categoryManagement");

const productManagement = () =>
    import ("../views/access/productManagement");

const deviceManagement = () =>
    import ("../views/access/deviceManagement/device.vue");

const administrativeArea = () =>
    import ("../views/baiyunArea/administrativeArea/index.vue");

const monitoringPoint = () =>
    import ("../views/baiyunArea/monitoringPoint/index.vue");

const deviceListByBaiyunqu = () =>
    import ("../views/baiyunArea/deviceList/index.vue");

const enterpriseRole = () =>
    import ("../views/enterprise/role/role.vue");

const enterpriseUnit = () =>
    import ("../views/enterprise/unit/unit.vue");

const enterpriseOrg = () =>
    import ("../views/enterprise/org/org.vue");

const enterpriseOrgDetail = () =>
    import ("../views/enterprise/org/detail.vue");

const enterpriseMember = () =>
    import ("../views/enterprise/member/member.vue");

const deviceHost = () =>
    import ("../views/deviceList/host/index.vue");

const fireControlRoom = () =>
    import ("../views/fire/controlRoom/index.vue");

const fireControlRoomDatav = () =>
    import ("../views/fire/datav/index.vue");

const fpdManage = () =>
    import ("../views/fpdManage/fpdManage.vue");

const inspItems = () =>
    import ("../views/inspSvc/inspItems/inspItems.vue");

const inspPoints = () =>
    import ("../views/inspSvc/inspPoints/inspPoints.vue");

const inspTasks = () =>
    import ("../views/inspSvc/inspTasks/inspTasks.vue");

const homePermission = {
    path: "/home",
    name: "home",
    component: home,
};
// const datavPermission = {
//     path: "/datav",
//     name: "datav",
//     component: datav,
// };
const orgListPermission = {
    path: "/org/list",
    name: "orgList",
    component: orgList,
};
const orgPlanarPermission = {
    path: "/org/planar",
    name: "planar",
    component: planar,
};
const permissionListPermission = {
    path: "/system/permission",
    name: "permission",
    component: permission,
};
const alarmReceiverListPermission = {
    path: "/alarmReceiver/list",
    name: "alarmReceiver",
    component: alarmReceiver,
};

const warnListPermission = {
    path: "/alarm/list",
    name: "alarm",
    component: alarm,
};
// const villageManageListPermission = {
//     path: '/villageManage/list',
//     name: 'villageManage',
//     component: villageManage,
// };
const buildingsManageListPermission = {
    path: "/buildingsManage/list",
    name: "buildingsManage",
    component: buildingsManage,
};
const drawingListPermission = {
    path: "/drawing/list",
    name: "drawing",
    component: drawing,
};
const floorsManageListPermission = {
    path: "/floorsManage/list",
    name: "floorsManage",
    component: floorsManage,
};
// const mountPointsDefineListPermission = {
//     path: "/mountPointsDefine/list",
//     name: "mountPointsDefine",
//     component: mountPointsDefine,
// };
const spacesManageListPermission = {
    path: "/spacesManage/list",
    name: "spacesManage",
    component: spacesManage,
};
// const isolateManageListPermission = {
//     path: '/isolateManage/list',
//     name: 'isolateManage',
//     component: isolateManage,
// };
// const locateMonitorListPermission = {
//     path: '/locateMonitor',
//     name: 'locateMonitor',
//     component: locateMonitor,
// };
const logLoginListPermission = {
    path: "/log/logininfor",
    name: "logLogin",
    component: logLogin,
};
const wxMpLogListPermission = {
    path: "/log/pushlog",
    name: "logPushlog",
    component: logPushlog,
};
const installLogListPermission = {
    path: "/log/install",
    name: "installLog",
    component: installLog,
};
const logOperateListPermission = {
    path: "/log/operlog",
    name: "logOperate",
    component: logOperate,
};
const roleListPermission = {
    path: "/system/role",
    name: "role",
    component: role,
};
const memberListPermission = {
    path: "/system/member",
    name: "member",
    component: member,
};

const deviceListListPermission = {
    path: "/device/list",
    name: "deviceList",
    component: deviceList,
};
const tenantListListPermission = {
    path: "/tenant/list",
    name: "tenant",
    component: tenant,
};

const siteSettingListPermission = {
    path: "/system/site",
    name: "systemSite",
    component: systemSite,
};

const servicePermission = {
    path: "/message/service",
    name: "serviceList",
    component: serviceList,
};

const serviceVoicePermission = {
    path: "/message/voice",
    name: "voiceList",
    component: voiceList,
};

const voiceLogPermission = {
    path: "/message/voiceLog",
    name: "voiceLogList",
    component: voiceLogList,
};

const smsLogPermission = {
    path: "/message/smsLog",
    name: "smsLogList",
    component: smsLogList,
};

const systemSetupListPermission = {
    path: "/system/setup",
    name: "systemSetup",
    component: systemSetup,
};

const devicePermission = {
    path: "/device/product",
    name: "product",
    component: product,
};
const regionPermission = {
    path: "/region/list",
    name: "region",
    component: region,
};
const manufacturerPermission = {
    path: "/device/manufacturer",
    name: "manufacturer",
    component: manufacturer,
};
const deviceEndListPermission = {
    path: "/device/terminal",
    name: "terminal",
    component: terminal,
};

const smokeDetectorListPermission = {
    path: "/device/smoke",
    name: "smoke",
    component: smoke,
};

const commandServiceListPermission = {
    path: "/device/command",
    name: "command",
    component: command,
};

const hidDangerListPermission = {
    path: "/alarm/hidden",
    name: "hidden",
    component: hidden,
};

const protectConfigListPermission = {
    path: "/maintenance/list",
    name: "maintenance",
    component: maintenance,
};

const exportTaskListPermission = {
    path: "/system/task",
    name: "exportTask",
    component: exportTask,
};

const dataExportPermission = {
    path: "/export/export",
    name: "dataExport",
    component: dataExport,
};

const dictPermission = {
    path: "/system/dict",
    name: "dict",
    component: dict,
};

const pushConfigurationListPermission = {
    path: "/subscribe/subscribeList",
    name: "subscribe",
    component: subscribe,
};

const logpushListPermission = {
    path: "/log/subscribe",
    name: "logpush",
    component: logpush,
};

const workOrderListPermission = {
    path: "/maintenance/workOrder",
    name: "workOrder",
    component: workOrder,
};

const alarmworkOrderPermission = {
    path: "/alarm/workOrder",
    name: "alarmworkOrder",
    component: alarmworkOrder,
};

const ruleEnginePermission = {
    path: "/ruleEngine/rule",
    name: "rule",
    component: rule,
};

const alarmPermission = {
    path: "/statics/alarm",
    name: "alarmStatics",
    component: alarmStatics,
};

const hiddenDangerPermission = {
    path: "/statics/hiddenDanger",
    name: "hiddenDangerStatics",
    component: hiddenDangerStatics,
};

const categoryManagementPermission = {
    path: "/access/categoryManagement",
    name: "categoryManagement",
    component: categoryManagement,
};

const productManagementPermission = {
    path: "/access/productManagement",
    name: "productManagement",
    component: productManagement,
};

const deviceManagementPermission = {
    path: "/access/deviceManagement",
    name: "deviceManagement",
    component: deviceManagement,
};

const administrativeAreaPermission = {
    path: "/baiyunArea/administrativeArea",
    name: "administrativeArea",
    component: administrativeArea,
};

const monitoringPointPermission = {
    path: "/baiyunArea/monitoringPoint",
    name: "monitoringPoint",
    component: monitoringPoint,
};

const deviceListPermission = {
    path: "/baiyunArea/deviceList",
    name: "deviceList",
    component: deviceListByBaiyunqu,
};

const enterpriseUnitPermission = {
    path: "/unit/list",
    name: "enterpriseUnit",
    component: enterpriseUnit,
};

const enterpriseRolePermission = {
    path: "/enterprise/role",
    name: "enterpriseRole",
    component: enterpriseRole,
};

const enterpriseOrgPermission = {
    path: "/enterprise/org",
    name: "enterpriseOrg",
    component: enterpriseOrg,
};

const enterpriseMemberPermission = {
    path: "/enterprise/member",
    name: "enterpriseMember",
    component: enterpriseMember,
};

const deviceHostPermission = {
    path: "/device/host",
    name: "deviceHost",
    component: deviceHost,
};

const fireControlRoomPermission = {
    path: "/fire/controlRoom",
    name: "fireControlRoom",
    component: fireControlRoom,
};

const fpdManagePermission = {
    path: "/fpd/manage",
    name: "fpdManage",
    component: fpdManage,
};

const inspItemsPermission = {
    path: "/Inspection/items",
    name: "inspItems",
    component: inspItems,
};

const inspPointsPermission = {
    path: "/Inspection/points",
    name: "inspPoints",
    component: inspPoints,
};

const inspTasksPermission = {
    path: "/Inspection/tasks",
    name: "inspTasks",
    component: inspTasks,
};

const ruleMapping = {
    "/org/list": orgListPermission,
    "/org/planar": orgPlanarPermission,
    "/home": homePermission,
    // "/datav": datavPermission,
    "/system/permission": permissionListPermission,
    "/alarmReceiver/list": alarmReceiverListPermission,
    "/alarm/list": warnListPermission,
    // '/villageManage/list': villageManageListPermission,
    "/buildingsManage/list": buildingsManageListPermission,
    "/drawing/list": drawingListPermission,
    "/floorsManage/list": floorsManageListPermission,
    // "/mountPointsDefine/list": mountPointsDefineListPermission,
    "/spacesManage/list": spacesManageListPermission,
    // '/isolateManage/list': isolateManageListPermission,
    // '/locateMonitor': locateMonitorListPermission,
    "/log/logininfor": logLoginListPermission,
    "/log/pushlog": wxMpLogListPermission,
    "/log/install": installLogListPermission,
    "/log/operlog": logOperateListPermission,
    "/system/role": roleListPermission,
    "/system/member": memberListPermission,
    "/device/list": deviceListListPermission,
    "/tenant/list": tenantListListPermission,
    "/system/site": siteSettingListPermission,
    "/device/terminal": deviceEndListPermission,
    "/device/smoke": smokeDetectorListPermission,
    "/device/command": commandServiceListPermission,
    "/message/service": servicePermission,
    "/message/voice": serviceVoicePermission,
    "/message/voiceLog": voiceLogPermission,
    "/message/smsLog": smsLogPermission,
    "/system/setup": systemSetupListPermission,
    "/device/product": devicePermission,
    "/region/list": regionPermission,
    "/device/manufacturer": manufacturerPermission,
    "/alarm/hidden": hidDangerListPermission,
    "/maintenance/list": protectConfigListPermission,
    "/system/task": exportTaskListPermission,
    "/export/export": dataExportPermission,
    "/system/dict": dictPermission,
    "/subscribe/subscribeList": pushConfigurationListPermission,
    "/log/subscribe": logpushListPermission,
    "/maintenance/workOrder": workOrderListPermission,
    "/alarm/workOrder": alarmworkOrderPermission,
    "/ruleEngine/rule": ruleEnginePermission,
    "/statics/alarm": alarmPermission,
    "/statics/hiddenDanger": hiddenDangerPermission,
    "/access/categoryManagement": categoryManagementPermission,
    "/access/productManagement": productManagementPermission,
    "/access/deviceManagement": deviceManagementPermission,
    "/baiyunArea/administrativeArea": administrativeAreaPermission,
    "/baiyunArea/monitoringPoint": monitoringPointPermission,
    "/baiyunArea/deviceList": deviceListPermission,
    "/enterprise/role": enterpriseRolePermission,
    "/unit/list": enterpriseUnitPermission,
    "/enterprise/org": enterpriseOrgPermission,
    "/enterprise/member": enterpriseMemberPermission,
    "/device/host": deviceHostPermission,
    "/fire/controlRoom": fireControlRoomPermission,
    "/fpd/manage": fpdManagePermission,
};

const routes = [{
        path: "/login",
        name: "login",
        component: login,
    },
    {
        path: "/",
        name: "index",
        // redirect: '/system/member',
        component: index,
        // redirect: "/home",
        children: [{
                path: "/inspection/items",
                name: "inspItems",
                component: inspItems,
            },
            {
                path: "/inspection/points",
                name: "inspPoints",
                component: inspPoints,
            },
            {
                path: "/inspection/tasks",
                name: "inspTasks",
                component: inspTasks,
            },
            {
                path: "/mountPointsDefine/list",
                name: "mountPointsDefine",
                component: mountPointsDefine,
            },
            {
                path: "/personal/list",
                name: "personal",
                component: personal,
            },
            {
                path: "/productType/list",
                name: "productType",
                component: productType,
                hidden: true,
                children: [{
                    path: "index/:dictId(\\d+)",
                    component: () =>
                        import ("@/views/productType/productType"),
                    name: "productType",
                    meta: { title: "产品详情", activeMenu: "/device/product" },
                }, ],
            },
            {
                path: "/system/dictData",
                name: "dictData",
                component: dictData,
                hidden: true,
                children: [{
                    path: "index/:dictId(\\d+)",
                    component: () =>
                        import ("@/views/system/dict-data/dictData"),
                    name: "dictData",
                    meta: { title: "字典数据", activeMenu: "/system/dict" },
                }, ],
            },
            {
                path: "/alarmDetails/list",
                name: "alarmDetails",
                component: alarmDetails,
            },
            {
                path: "/datav",
                name: "datav",
                component: datav,
            },
            {
                path: "/fire/controlRoom/datav",
                name: "fireControlRoomDatav",
                component: fireControlRoomDatav,
            },
            {
                path: "/mainframe",
                name: "mainframe",
                component: mainframe,
            },
            {
                path: "/building/details",
                name: "buildingDetails",
                component: buildingDetails,
            },
            {
                path: "/video/surveillance",
                name: "videoSurveillance",
                component: videoSurveillance,
            },
            {
                path: "/org/details",
                name: "enterpriseOrgDetail",
                component: enterpriseOrgDetail,
            },
            {
                path: "fire/controlRoom",
                component: () =>
                    import ("@/views/fire/controlRoom/index"),
                name: "fireControlRoom",
            },
        ],
    },
    {
        path: "/account1Webpage2",
        name: "accountWebpage",
        component: accountWebpage,
    },
    {
        path: "/liveMacrovision",
        name: "liveMacrovision",
        component: liveMacrovision,
    },
    {
        path: "/ceshi",
        name: "ceshi",
        component: ceshi,
    },
    {
        path: "/loginProtocol",
        name: "loginProtocol",
        component: loginProtocol,
    },
    {
        path: "/serviceTerms",
        name: "serviceTerms",
        component: serviceTerms,
    },
    {
        path: "/privacy",
        name: "privacy",
        component: privacy,
    },
    {
        path: "/HomeView",
        name: "HomeView",
        component: HomeView,
    },
    {
        path: "/account1Webpage2",
        name: "accountWebpage",
        component: accountWebpage,
    },
    {
        path: "/liveMacrovision",
        name: "liveMacrovision",
        component: liveMacrovision,
    },
    {
        path: "/ceshi",
        name: "ceshi",
        component: ceshi,
    },
    {
        path: "/loginProtocol",
        name: "loginProtocol",
        component: loginProtocol,
    },
    {
        path: "/serviceTerms",
        name: "serviceTerms",
        component: serviceTerms,
    },
    {
        path: "/privacy",
        name: "privacy",
        component: privacy,
    },
    {
        path: "/HomeView",
        name: "HomeView",
        component: HomeView,
    },
    {
        path: "*",
        name: "notFound",
        component: notFound,
    },
];

const router = new VueRouter({
    mode: "history", // 去掉url中的#
    routes,
});

/* 路由导航守卫 */
router.beforeEach((to, from, next) => {
    // if (to.path === '/login' || to.path === '/forget-password') {
    //     next();
    // } else {
    //     // // getParams();
    //     // if (!window.sessionStorage.getItem('token')) {
    //     //     next('/login');
    //     // }
    //     // //  else if (to.fullPath.indexOf("?") != -1) {
    //     // //     store.commit('saveRouteState', true)
    //     // //     router.push('/')
    //     // // }
    //     // else {
    //     //     next();
    //     // }
    //     next();
    // }

    // 地图引入
    if (to.path == "/datav" && !store.state.user.isExsistMap) {
        var script = document.createElement("script");
        script.src =
            "https://map.qq.com/api/gljs?v=1.exp&key=6YRBZ-CE263-TES33-YGBKX-AJ2SH-I4FJH";
        document.getElementsByTagName("head")[0].appendChild(script);

        // 大屏mqtt按需引入
        // var script2 = document.createElement('script');
        // script2.src = 'https://cdn.bootcdn.net/ajax/libs/mqtt/4.1.0/mqtt.min.js';
        // document.getElementsByTagName('head')[0].appendChild(script2);

        store.commit("saveIsExsitMap", true);
    }

    if (
        to.path === "/login" ||
        to.path === "/ceshi" ||
        to.path === "/account1Webpage2" ||
        to.path === "/loginProtocol" ||
        to.path === "/serviceTerms" ||
        to.path === "/privacy" ||
        to.path === "/HomeView" ||
        to.path === "/liveMacrovision"
    ) {
        //不同页面展示不同的title
        if (to.path === "/loginProtocol") {
            document.title = "第三方账号创建登录协议"; //不同页面展示不同的title
        } else if (to.path === "/serviceTerms") {
            document.title = "服务条款";
        } else if (to.path === "/privacy") {
            document.title = "法律声明及隐私权政策";
        } else if (to.path === "/HomeView") {
            document.title = "设备详情";
        } else if (to.path === "/liveMacrovision") {
            document.title = "设备详情";
        }
        next();
    } else {
        if (!window.sessionStorage.getItem("token")) {
            next("/login");
        } else {
            next();
        }
    }
});

/* 配置动态路由 */
export const initDynamicRoutes = () => {
    const currentRoutes = router.options.routes; //已配置的路由
    const permission = store._modules.root.state.user.permissions;
    // console.log(currentRoutes);
    // console.log(asc.decrypt(permission));
    // console.log(permission);
    permission.forEach((item) => {
        // 有子级
        if (item.children.length) {
            item.children.forEach((citem) => {
                let citemRule = ruleMapping[citem.url]; //添加路由权限
                let Right = [];
                citem.children.forEach((ccitem) => {
                    Right.push(ccitem.value);
                });
                // console.log(citemRule);
                citemRule.meta = Right; //添加按钮权限
                currentRoutes[1].children.push(citemRule);
            });
        } else {
            // 无子级
            let citemRule;
            if (ruleMapping[item.url]) {
                citemRule = ruleMapping[item.url];
                currentRoutes[1].children.push(citemRule);
            }
        }
    });
    router.addRoutes(currentRoutes); //添加路由规则
};

// /* 路由导航守卫 */
// router.beforeEach((to, from, next) => {
//     next();
//     // if (to.path === '/login') {
//     //       next();
//     // } else {
//     //     getParams();

//     //     if (!window.sessionStorage.getItem('token')) {
//     //         next('/login');
//     //     } else if (to.fullPath.indexOf('?') != -1) {
//     //         // store.commit('saveRouteState', true);
//     //         router.push('/define');
//     //     } else {
//     //         next();
//     //     }
//     // }
// });

// 这段代码实际上是用来捕获Vue Router的push方法抛出的任何错误，并将其静默地忽略。这样的做法在某些情况下可能是有用的，例如，当你希望在路由变化失败时不显示任何错误信息。
// const VueRouterPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(to) {
//     return VueRouterPush.call(this, to).catch(err => err);
// };

export default router;